/* ---------------------------------------------------------------------------------------------
 *
 *            Capital Alliance Software Confidential Proprietary
 *            (c) Copyright CAS 201{x}, All Rights Reserved
 *                          www.pekall.com
 *
 * ----------------------------------------------------------------------------------------------
 */
package com.pekall.todo;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.pekall.todo.model.ToDoTasks;
import com.pekall.todo.util.SQLiteOperate;

/**
 * 待办事件删除界面
 * 
 * @author LeiKang
 */
public class DeleteScreen extends Activity implements OnClickListener {
	private ListView deleteList;
	private List<ToDoTasks> mToDoTaskses;
	private ListAdapter mAdapter;
	// private ImageButton deleteBtn;
	private CheckBox selectAllBtn;
	private static final int STATE_COMPLETE = 1;
	private static final int STATE_UNCOMPLETE = 0;
	private static final int LEVEL_LOW = 0;
	private static final int LEVEL_MID = 1;
	private static final int LEVEL_HIGH = 2;
	private boolean isSelectAll = true;// 是否为全选

	/* ======================= activity ========================= */
	@SuppressWarnings("unchecked")
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.deletescreen);
		deleteList = (ListView) findViewById(R.id.deletelist);
		// deleteBtn = (ImageButton) findViewById(R.id.delete);
		selectAllBtn = (CheckBox) findViewById(R.id.selectAll);
		// deleteBtn.setOnClickListener(this);
		selectAllBtn.setOnClickListener(this);
		Intent intent = getIntent();
		mToDoTaskses = (ArrayList<ToDoTasks>) intent
				.getSerializableExtra("todotasks_key");
		mAdapter = new ListAdapter(this, mToDoTaskses);
		deleteList.setAdapter(mAdapter);
	}

	public void onClick(View v) {
		// if (v.getId() == R.id.delete) {
		// Dialog dialog = new AlertDialog.Builder(this).setIcon(
		// android.R.drawable.btn_star).setTitle(R.string._delete)
		// .setMessage(R.string._delete_message).setNegativeButton(
		// R.string._cancle, null).setNeutralButton(
		// R.string._ok,
		// new DialogInterface.OnClickListener() {
		// public void onClick(DialogInterface dialog,
		// int whichButton) {
		// delete();
		// }
		// }).create();
		//
		// // 显示对话框
		// dialog.show();
		// // delete();
		// } else
		if (v.getId() == R.id.selectAll) {

			for (int i = 0; i < mToDoTaskses.size(); i++) {
				mToDoTaskses.get(i).setChecked(isSelectAll);
			}
			if (isSelectAll) {
				isSelectAll = false;
				selectAllBtn.setChecked(true);
			} else {
				isSelectAll = true;
				selectAllBtn.setChecked(false);
			}
			deleteList.setAdapter(mAdapter);
		}
	}

	private void delete() {
		SQLiteOperate sql = new SQLiteOperate(this);
		for (int i = 0; i < mToDoTaskses.size(); i++) {
			if (mToDoTaskses.get(i).isChecked())
				sql.deleteById(mToDoTaskses.get(i).getId());
		}
		finish();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, Menu.FIRST + 1, Menu.NONE, R.string._delete).setIcon(
				R.drawable.cmcc_toolbar_del);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		switch (item.getItemId()) {
		case Menu.FIRST + 1:
			int count = 0;
			for (int i = 0; i < mToDoTaskses.size(); i++) {
				if (mToDoTaskses.get(i).isChecked())
					 count++;
			}
			if(count==0){
				Toast.makeText(this, R.string._no_select, Toast.LENGTH_SHORT).show();
				break;
			}
			Dialog dialog = new AlertDialog.Builder(this).setIcon(
					android.R.drawable.btn_star).setTitle(R.string._delete)
					.setMessage(R.string._delete_message).setNegativeButton(
							R.string._cancle, null).setNeutralButton(
							R.string._ok,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									delete();
								}
							}).create();

			// 显示对话框
			dialog.show();
			break;
		}
		return true;
	}

	/* ======================= adapter ========================= */
	public class ListAdapter extends BaseAdapter {
		private List<ToDoTasks> mToDotaskses;
		private Context mContext;
		private LayoutInflater mLayoutInflater;

		private ListAdapter(Context context, List<ToDoTasks> toDotaskses) {
			mContext = context;
			mLayoutInflater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			mToDotaskses = toDotaskses;
		}

		public int getCount() {
			return mToDotaskses.size();
		}

		public Object getItem(int position) {
			return mToDotaskses.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = mLayoutInflater.inflate(R.layout.delete_item,
						null);
			}
			final LinearLayout linearLayout = (LinearLayout) convertView;
			final ImageView stateIcon = (ImageView) linearLayout
					.findViewById(R.id.stateIcon);
			final TextView eventTitle = (TextView) linearLayout
					.findViewById(R.id.eventTitle);
			final ImageView levelIcon = (ImageView) linearLayout
					.findViewById(R.id.levelIcon);
			final CheckBox checkBox = (CheckBox) linearLayout
					.findViewById(R.id.CheckBox_checklayout);
			checkBox
					.setOnCheckedChangeListener(new MyCheckBoxListener(position));
			checkBox.setChecked(mToDotaskses.get(position).isChecked());
			final int state = mToDotaskses.get(position).getState();
			final int type = mToDotaskses.get(position).getType();

			if (state == STATE_COMPLETE) {
				stateIcon.setImageResource(R.drawable.cmcc_list_completed);
				levelIcon.setImageDrawable(null);

			} else if (state == STATE_UNCOMPLETE) {
				stateIcon
						.setImageResource(R.drawable.cmcc_todolist_uncompleted);
				if (type == LEVEL_LOW)
					levelIcon
							.setImageResource(R.drawable.cmcc_list_priority_low);
				else if (type == LEVEL_MID)
					levelIcon
							.setImageResource(R.drawable.cmcc_list_priority_medium);
				else if (type == LEVEL_HIGH)
					levelIcon
							.setImageResource(R.drawable.cmcc_list_priority_high);
			}
			eventTitle.setText(mToDotaskses.get(position).getTitle());

			return linearLayout;
		}

	}

	/* ======================= adapter listener========================= */
	private class MyCheckBoxListener implements OnCheckedChangeListener {
		private int mPosition;

		private MyCheckBoxListener(int position) {
			mPosition = position;
		}

		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			mToDoTaskses.get(mPosition).setChecked(isChecked);
		}
	}
}
